using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Sale_and_manage
{
    public partial class frmManageCustomer : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmManageCustomer()
        {
            InitializeComponent();
            LoadAllCustomer();
            CreateData();
        }

        public List<CustomerView> lcv = new List<CustomerView>();
        public CustomerDAL cDAL = new CustomerDAL();
        public CustomerView currentCustomer;
        public List<int> thangNamthuong = new List<int>();
        public List<int> thangNamnhuan = new List<int>();
        public bool namNhuan = false;
        public Int32 filterCMND = 0;
        public int filterYear = 0;
        public int filterMonth = 0;
        public int filterDay = 0;
        public string filterName = "";
        public bool point = true;
        public bool name = true;

        public void CreateData()
        {
            cboYear.Items.Add("");
            for (int i = DateTime.Now.Year;i>=1800 ; i--)
            {
                cboYear.Items.Add(i);
            }
            for (int i = 1; i <= 12; i++)
            {
                thangNamnhuan.Add(DateTime.DaysInMonth(2012, i));
                thangNamthuong.Add(DateTime.DaysInMonth(2011, i));
            }
            cboYear.SelectedIndex = 0;
        }

        public void LoadAllCustomer()
        {
            lcv = cDAL.GetAllCustomerToCV();
            if (lcv != null)
            {

                dgvCustomer.DataSource = lcv;
                dgvCustomer.Columns[0].Visible = false;
                dgvCustomer.Columns[1].Width = 67;
                dgvCustomer.Columns[2].Width = 150;
                dgvCustomer.Columns[3].Width = 67;
                dgvCustomer.Columns[4].Width = 270;
                dgvCustomer.Columns[5].Width = 120;
                dgvCustomer.Columns[6].Width = 77;
                dgvCustomer.Columns[7].Width = 77;
                dgvCustomer.Columns[8].Width = 70;
            }
            else
            {
                dgvCustomer.DataSource = new List<Customer>();
                dgvCustomer.Columns[0].Visible = false;
                dgvCustomer.Columns[1].Width = 67;
                dgvCustomer.Columns[2].Width = 150;
                dgvCustomer.Columns[3].Width = 67;
                dgvCustomer.Columns[4].Width = 270;
                dgvCustomer.Columns[5].Width = 120;
                dgvCustomer.Columns[6].Width = 77;
                dgvCustomer.Columns[7].Width = 77;
                dgvCustomer.Columns[8].Width = 70;
            }
        }

        public void CustomerFilter()
        { }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboYear.SelectedIndex == 0)
            {
                filterYear = 0;
            }
            else
            {
                filterYear = Convert.ToInt16(cboYear.Text);
                int nam = Int32.Parse(cboYear.Text);
                if ((nam % 4 == 0 && nam % 100 != 0) || nam % 400 == 0)
                {
                    namNhuan = true;
                }
            }
            cboDay.Items.Clear();
            cboMonth.SelectedIndex = cboMonth.SelectedIndex;
            LoadCustomerByFilter();
        }

        public void LoadCustomerByFilter()
        {
            highlighter1.SetHighlightColor(btnSortbyBonus, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter1.SetHighlightColor(btnSortbyName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            CustomerDAL cDAL = new CustomerDAL();
            lcv = cDAL.GetAllCustomerByFilterToCV(filterCMND, filterName, filterMonth, filterYear, filterDay);
            if (lcv == null || lcv.Count == 0)
            {
                dgvCustomer.DataSource = new List<CustomerView>();
                dgvCustomer.Columns[0].Visible = false;
                dgvCustomer.Refresh();
            }
            else
            {
                dgvCustomer.DataSource = lcv;
                dgvCustomer.Columns[0].Visible = false;
                dgvCustomer.Columns[1].Width = 67;
                dgvCustomer.Columns[2].Width = 150;
                dgvCustomer.Columns[3].Width = 67;
                dgvCustomer.Columns[4].Width = 270;
                dgvCustomer.Columns[5].Width = 120;
                dgvCustomer.Columns[6].Width = 77;
                dgvCustomer.Columns[7].Width = 77;
                dgvCustomer.Columns[8].Width = 70;
                dgvCustomer.Refresh();
            }
        }

        private void txtCustomerID_TextChanged(object sender, EventArgs e)
        {
            if (txtCustomerID.Text == "")
            {
                filterCMND = 0;
            }
            else
            { 
                filterCMND = Convert.ToInt32(txtCustomerID.Text);
            }
            LoadCustomerByFilter();
        }

        private void txtCustomerID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboMonth.SelectedIndex == 0)
            {
                cboDay.Enabled = false;
                filterMonth = 0; 
                cboDay.Items.Clear();
            }
            else
            {
                
                filterMonth = cboMonth.SelectedIndex;
                cboDay.Items.Clear();
                if (cboYear.SelectedIndex != 0)
                {
                    cboDay.Enabled = true;
                    cboDay.Items.Add("");
                    if (namNhuan)
                    {
                        for (int i = 1; i <= thangNamnhuan[filterMonth-1]; i++)
                        {
                            cboDay.Items.Add(i);
                        }
                        cboDay.SelectedIndex = 0;
                    }
                    else
                    {
                        for (int i = 1; i <= thangNamthuong[filterMonth]; i++)
                        {
                            cboDay.Items.Add(i);
                        }
                        cboDay.SelectedIndex = 0;
                    }

                }
                else
                {
                    cboDay.Enabled = false;
                }
            }
            LoadCustomerByFilter();
        }

        private void cboDay_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboDay.SelectedIndex == 0)
            {
                filterDay = 0;
            }
            else
            {
                filterDay = cboDay.SelectedIndex;
            }
            LoadCustomerByFilter();
        }

        private void txtCustomerName_TextChanged(object sender, EventArgs e)
        {
            filterName = txtCustomerName.Text;
            LoadCustomerByFilter();
        }

        private void btnClearFilter_Click(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(btnSortbyBonus, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter1.SetHighlightColor(btnSortbyName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            txtCustomerID.Clear();
            txtCustomerName.Clear();
            cboDay.Items.Clear();
            cboDay.Enabled = false;
            cboYear.SelectedIndex = 0;
            cboMonth.SelectedIndex = 0;
            LoadAllCustomer();
        }

        private void btnSortbyBonus_Click(object sender, EventArgs e)
        {
            point = !point;
            if (point)
            {
                lcv.Sort(new LowPoint());
                dgvCustomer.DataSource = lcv;
                dgvCustomer.Refresh();
                highlighter1.SetHighlightColor(btnSortbyBonus, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                highlighter1.SetHighlightColor(btnSortbyName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            }
            else
            {
                lcv.Sort(new HighPoint());
                dgvCustomer.DataSource = lcv;
                dgvCustomer.Refresh();

                highlighter1.SetHighlightColor(btnSortbyName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
                highlighter1.SetHighlightColor(btnSortbyBonus, DevComponents.DotNetBar.Validator.eHighlightColor.Green);
            }
        }

        private void btnSortbyName_Click(object sender, EventArgs e)
        {
            name = !name;
            if (name)
            {
                lcv.Sort(new LowName());
                dgvCustomer.DataSource = lcv;
                dgvCustomer.Refresh();
                highlighter1.SetHighlightColor(btnSortbyBonus, DevComponents.DotNetBar.Validator.eHighlightColor.None);
                highlighter1.SetHighlightColor(btnSortbyName,DevComponents.DotNetBar.Validator.eHighlightColor.Red);
            }
            else
            {
                lcv.Sort(new HighName());
                dgvCustomer.DataSource = lcv;
                dgvCustomer.Refresh();
                highlighter1.SetHighlightColor(btnSortbyBonus, DevComponents.DotNetBar.Validator.eHighlightColor.None);
                highlighter1.SetHighlightColor(btnSortbyName, DevComponents.DotNetBar.Validator.eHighlightColor.Green);
            }
        }

        
        private void tabSearch_Click(object sender, EventArgs e)
        {
            LoadAllCustomer();
            dgvCustomer.Enabled = true;
            txtEditAddress.Clear();
            txtEditCellPhone.Clear();
            txtEditCustomerID.Clear();
            txtEditEmail.Clear();
            txtEditName.Clear();
            txtEditPhone.Clear();
            cboEditBirthday.ResetText();
        }

        private void tabEdit_Click(object sender, EventArgs e)
        {
            dgvCustomer.Enabled = false;
            if (currentCustomer.Birthday == "N/A")
            {
                cboEditBirthday.WatermarkText = "N/A";
            }
            else
            {
                cboEditBirthday.Text = currentCustomer.Birthday;
            }
            txtEditAddress.Text = currentCustomer.Address;
            if (currentCustomer.Tel == "N/A")
            {
                txtEditCellPhone.WatermarkText = "N/A";
            }
            else
            {
                txtEditCellPhone.Text = currentCustomer.Tel;
            }
            txtEditCustomerID.Text = currentCustomer.ID.ToString();
            if (currentCustomer.Email == "N/A")
            {
                txtEditEmail.WatermarkText = "N/A";
            }
            else
            {
                txtEditEmail.Text = currentCustomer.Email;
            }
            txtEditName.Text = currentCustomer.Name;
            if (currentCustomer.Phone == "N/A")
            {
                txtEditPhone.WatermarkText = "N/A";
            }
            else
            {
                txtEditPhone.Text = currentCustomer.Phone;
            }
        }

        private void tabFilterbyBook_Click(object sender, EventArgs e)
        {
            dgvCustomer.Enabled = false;

        }

        private void tabFilterbyBill_Click(object sender, EventArgs e)
        {
            dgvCustomer.Enabled = false;
        }

        private void dgvCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            currentCustomer = lcv[dgvCustomer.SelectedRows[0].Index];
        }

        private void frmManageCustomer_Load(object sender, EventArgs e)
        {
            currentCustomer = lcv[dgvCustomer.SelectedRows[0].Index];
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            txtEditAddress.Clear();
            txtEditCellPhone.Clear();
            txtEditCustomerID.Clear();
            txtEditEmail.Clear();
            txtEditName.Clear();
            txtEditPhone.Clear();
            cboEditBirthday.ResetText();
            if (currentCustomer.Birthday == "N/A")
            {
                cboEditBirthday.WatermarkText = "N/A";
            }
            else
            {
                cboEditBirthday.Text = currentCustomer.Birthday;
            }
            txtEditAddress.Text = currentCustomer.Address;
            if (currentCustomer.Tel == "N/A")
            {
                txtEditCellPhone.WatermarkText = "N/A";
            }
            else
            {
                txtEditCellPhone.Text = currentCustomer.Tel;
            }
            txtEditCustomerID.Text = currentCustomer.ID.ToString();
            if (currentCustomer.Email == "N/A")
            {
                txtEditEmail.WatermarkText = "N/A";
            }
            else
            {
                txtEditEmail.Text = currentCustomer.Email;
            }
            txtEditName.Text = currentCustomer.Name;
            if (currentCustomer.Phone == "N/A")
            {
                txtEditPhone.WatermarkText = "N/A";
            }
            else
            {
                txtEditPhone.Text = currentCustomer.Phone;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cboEditBirthday.Value == DateTime.MinValue)
            {
                cboEditBirthday.Value = DateTime.Parse("1/1/1800");
            }
            Customer c = new Customer(currentCustomer.CustomerID, Convert.ToInt32(txtEditCustomerID.Text), txtEditName.Text,
                                    cboEditBirthday.Value, txtEditAddress.Text, txtEditEmail.Text, txtEditPhone.Text, txtEditCellPhone.Text, currentCustomer.Points);
            cDAL.UpdateCustomer(c);
            if (cboEditBirthday.Value ==DateTime.Parse("1/1/1800"))
            {
                cboEditBirthday.ResetText();
            }
        }

        private void dgvCustomer_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCustomer.SelectedRows.Count > 0)
            {
                currentCustomer = lcv[dgvCustomer.SelectedRows[0].Index];
            }
        }
    }
}

public class LowPoint : IComparer<CustomerView>
{
    public int Compare(CustomerView x, CustomerView y)
    {
        if (x.Points > y.Points) return 1;
        else if (x.Points < y.Points) return -1;
        else return 0;
    }
}

public class HighPoint : IComparer<CustomerView>
{
    public int Compare(CustomerView x, CustomerView y)
    {
        if (x.Points < y.Points) return 1;
        else if (x.Points > y.Points) return -1;
        else return 0;
    }
}

public class HighName : IComparer<CustomerView>
{
    public int Compare(CustomerView x, CustomerView y)
    {
        if (string.Compare(x.Name,y.Name)>0) return 1;
        else if (string.Compare(x.Name, y.Name)<0) return -1;
        else return 0;
    }
}

public class LowName : IComparer<CustomerView>
{
    public int Compare(CustomerView x, CustomerView y)
    {
        if (string.Compare(x.Name, y.Name) < 0) return 1;
        else if (string.Compare(x.Name, y.Name) > 0) return -1;
        else return 0;
    }
}